package com.wcards.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wcards.config.R;
import com.wcards.entity.AdoptApplicationInfo;
import com.wcards.entity.AdoptInfo;
import com.wcards.entity.AdoptRecordInfo;
import com.wcards.entity.AnimalInfo;
import com.wcards.service.AdoptApplicationInfoService;
import com.wcards.service.AdoptInfoService;
import com.wcards.service.AdoptRecordInfoService;
import com.wcards.service.AnimalInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.List;

/**
 * @author q'y
 */
@Api("领养模块")
@RestController
@Slf4j
@RequestMapping("/adopt")
public class AdoptController {
    @Autowired
    private AdoptRecordInfoService adoptRecordInfoService;
    @Autowired
    private AnimalInfoService animalInfoService;
    @Autowired
    private AdoptInfoService adoptInfoService;
    @Autowired
    private AdoptApplicationInfoService adoptApplicationInfoService;

    @ApiOperation("查看所有申请")
    @GetMapping("/getApp")
    public R<R<Page<AdoptApplicationInfo>>> getApp(@ApiParam(value = "分页的当前页及分页大小",required = true) @RequestParam(defaultValue = "1") Integer current , @RequestParam(defaultValue ="10") Integer size)
    {
        R<Page<AdoptApplicationInfo>> pageR = adoptApplicationInfoService.listAll(current, size);
        return R.success(pageR);
    }

    @ApiOperation("根据申请id查看申请")
    @GetMapping("/getAppByNum")
    public R<AdoptApplicationInfo> getAppByNum(@ApiParam(value = "申请编号",required = true) @RequestParam(defaultValue = "1") Integer num )
    {
        return R.success(adoptApplicationInfoService.getById(num));
    }
    @ApiOperation("根据申请用户id查看本人申请表")
    @GetMapping("/getAppByUid")
    public R<Page<AdoptApplicationInfo> > getAppByUid(@ApiParam(value = "当前页")@RequestParam(defaultValue = "1") Integer current,
                                                      @ApiParam(value = "分页大小") @RequestParam(defaultValue = "10") Integer size,
                                                      @RequestParam(defaultValue = "1") Integer userId )
    {
        return R.success(adoptApplicationInfoService.listByUid(current,size,userId));
    }
    @ApiOperation("用户提交领养申请表")
    @PostMapping("/uAddApp")
    public R<String> addApp(@ApiParam(value = "必须需要申请人姓名，电话，理由，动物编号",required = true) @RequestBody AdoptApplicationInfo aaInfo,HttpServletRequest request)
    {
        return adoptApplicationInfoService.insertOne(aaInfo,request);
    }
    @ApiOperation("处理用户提交领养申请表")
    @PutMapping("/mModifyApp")
    public R<String> modifyApp(@ApiParam(value = "必须需要用户id，动物id，申请编号，处理结果状态值",required = true) @RequestBody AdoptApplicationInfo aaInfo)
    {

        return adoptApplicationInfoService.updateOne(aaInfo);
    }
    @ApiOperation("查看所有可领养动物")
    @GetMapping("getAnimal")
    public R<Page<AnimalInfo>> getAnimal(Integer pageSize, Integer pageNumber)
    {
        return animalInfoService.listAdopt(pageSize, pageNumber);
    }
    @ApiOperation("查看所有已领养动物")
    @GetMapping("getAdopted")
    public R<Page<AnimalInfo>> getAdopted(Integer pageSize, Integer pageNumber)
    {
        return animalInfoService.listAdopted(pageSize,pageNumber);
    }

    @ApiOperation("查看所有领养信息")
    @GetMapping("/getAdopt")
    public R<Page<AdoptInfo>> getApp(@ApiParam(value = "分页的当前页及分页大小",required = true) @RequestParam(defaultValue = "1") Long current, @RequestParam(defaultValue = "10") Long size)
    {
        return adoptInfoService.getAdoptInfo(current, size);
    }
    @ApiOperation("管理员修改领养信息")
    @PutMapping("/mUpdateAdopt")
    public R<Boolean> mUpdateAdopt(@ApiParam(value = "领养data，不需要领养编号",required = true) @RequestBody AdoptInfo adoptInfo)
    {
        return R.success(adoptInfoService.updateById(adoptInfo));
    }
    @ApiOperation("管理员新增领养信息")
    @PostMapping("/mAddAdopt")
    public R<Boolean> maApp(@ApiParam(value = "领养data，不需要领养编号，编号自增",required = true) @RequestBody AdoptInfo adoptInfo)
    {
        return R.success(adoptInfoService.save(adoptInfo));
    }
    @ApiOperation("管理员根据领养编号查看领养信息")
    @GetMapping("/mGetByNum")
    public R<AdoptInfo> mGetByAdoptNum(@ApiParam(value = "通过领养编号查询",required = true) @RequestParam Integer adoptNumbers)
    {
        return R.success(adoptInfoService.getById(adoptNumbers));
    }
    @ApiOperation("管理员根据领养编号批量删除")
    @DeleteMapping("/mDeleteByNums")
    public R<Boolean> mDeleteByNums(@ApiParam(value = "通过领养编号批量删除",required = true) @RequestParam Integer[] array)
    {
        List<Integer> adpotNumbersList = Arrays.asList(array);
        return R.success(adoptInfoService.removeBatchByIds(adpotNumbersList));
    }

    @ApiOperation("删除领养信息")
    @DeleteMapping("/deleteAdopt")
    public R<String> deleteAdopt(@ApiParam(value = "只需要领养编号",required = true) @RequestParam Integer aNumber)
    {
        return adoptInfoService.removeByAnum(aNumber);
    }
    @ApiOperation("用户查看领养记录")
    @GetMapping("/getRecord")
    public R<R<List<AdoptRecordInfo>>> getRecord(HttpServletRequest request,Integer userId)
    {
        R<List<AdoptRecordInfo>> listR = adoptRecordInfoService.listByUid(request,userId);
        return R.success(listR);
    }
    @PostMapping("/login")
    public String login(HttpServletRequest request){
        HttpSession session = request.getSession();
        System.out.println("存入session");
        Integer a=1;
        session.setAttribute("userId",a);
        return "ok";
    }

}
